//客户端
#include <sys/types.h> 
#include <sys/socket.h> 
#include <stdio.h> 
#include <netinet/in.h> 
#include <arpa/inet.h> 
#include <unistd.h> 
#include <stdlib.h>
#include <sys/time.h>
#include <sys/ioctl.h>

#include <string.h>

char buf[200];
int main() 
{ 
    int fd[2], len, port = 8000; 
    int i, result; 
    struct sockaddr_in address;//服务器端网络地址结构体 
    fd_set src_fds, res_fds; 
    for(i = 0; i < sizeof(fd)/sizeof(int); i++)
    {
        fd[i] = socket(AF_INET, SOCK_STREAM, 0);//建立客户端socket 
        address.sin_family = AF_INET; 
        address.sin_addr.s_addr = inet_addr("127.0.0.1");
        address.sin_port = htons(port); 
        len = sizeof(address); 
        port+=1;
        result = connect(fd[i], (struct sockaddr *)&address, len); 
        if(result == -1) 
        { 
             perror("oops: client2"); 
             exit(1); 
        } 
        printf("fd is %d \n", fd[i]);
    }

    FD_ZERO(&src_fds); 
    FD_SET(fd[0], &src_fds);
    FD_SET(fd[1], &src_fds);
    
    while(1) {
        res_fds = src_fds; /* select会将结果置入src_fds */
        select(FD_SETSIZE, &res_fds, NULL, NULL, NULL); 
        for(i = 0; i < sizeof(fd)/sizeof(int); i++) { 
            if(FD_ISSET(fd[i], &res_fds)) {
                int len = read(fd[i], buf, 200); 
                buf[len] = 0;
                printf("socket#%d rec %d: %s\n", i, len,  buf);
            }
        }   
    }
    return 0; 
}


